package org.darcy.controller.admin.site;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.darcy.entity.SysLink;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.LinkSourceEnum;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.blog.business.enums.TemplateKeyEnum;
import org.darcy.framework.blog.exception.BlogLinkException;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.RegexUtils;
import org.darcy.framework.util.ResultUtil;
import org.darcy.model.SysLinkModel;
import org.darcy.service.MailService;
import org.darcy.service.SysLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.StrUtil;

/**
 * 友情链接
 *
 */
@RestController
@RequestMapping("/link")
public class LinkController {
	
    @Autowired
    private SysLinkService linkService;
    
    @Autowired
    private MailService mailService;

    @RequiresPermissions("links")
	@PostMapping("/list")
	public PageResult list(SysLinkModel param) {
		Page p = new Page(param.getP(), param.getPageSize());
		QueryWrapper<SysLink> q = new QueryWrapper<SysLink>();
		if (StrUtil.isNotEmpty(param.getName()))
			q.like("name", param.getName());
		IPage pageInfo = linkService.getPage(p, q);
		return ResultUtil.tablePage(pageInfo);
	}

    @RequiresPermissions("link:add")
    @PostMapping(value = "/add")
    @BussinessLog("添加友情链接")
    public ResponseVO add(SysLink link) {
        link.setSource(LinkSourceEnum.ADMIN.name());
        if (!RegexUtils.isUrl(link.getUrl())) {
            throw new BlogLinkException("链接地址无效！");
        }
        linkService.insert(link);
        mailService.send(link, TemplateKeyEnum.TM_LINKS);
        return ResultUtil.success("成功");
    }

    @RequiresPermissions(value = {"link:batchDelete", "link:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    @BussinessLog("删除友情链接")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            linkService.delete(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个友情链接");
    }

    @RequiresPermissions("link:get")
    @PostMapping("/get/{id}")
    @BussinessLog("获取友情链接详情")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.linkService.getById(id));
    }

    @RequiresPermissions("link:edit")
    @PostMapping("/edit")
    @BussinessLog("编辑友情链接")
    public ResponseVO edit(SysLink link) {
        if (!RegexUtils.isUrl(link.getUrl())) {
            throw new BlogLinkException("链接地址无效！");
        }
        try {
            linkService.update(link);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("友情链接修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

}
